// --------------------------------------------------------------------------------+
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
// EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// --------------------------------------------------------------------------------+
if(typeof Cc == "undefined") { var Cc = Components.classes; }
if(typeof Ci == "undefined") { var Ci = Components.interfaces; }
if(typeof Cu == "undefined") { var Cu = Components.utils; }
	
	var cfxt = {
		_tab: ["chrome://browser/skin/tabbrowser/top/top.css","chrome://browser/skin/tabbrowser/basic/basic.css","chrome://browser/skin/tabbrowser/glass/glass.css"],
		_tabClose: ["chrome://browser/skin/tabbrowser/tabcloseactive.css","chrome://browser/skin/tabbrowser/tabclosenone.css"],

		init: function() {

			cfxt.tabStyle.apply();
		},
		
		unInit: function() {

		},
		
		tabStyle: {
			apply: function() {
				var tabMode = cfxp.tabMode.get();
				cfxp.previousTabMode.set(tabMode);
				document.getElementById("main-window").setAttribute("tabmode", tabMode);			
				switch(tabMode) {
					case 0: // tabs on top glass
						cfxg.glass.start();
						break;
					case 2: // tabs on top no glass
						cfxs.style.apply(cfxt._tab[0]);
						break;
					case 1: // basic mode
						cfxs.style.apply(cfxt._tab[1]);
						break;
					default:
						break;
				}
			},
			remove: function() {
				var tabMode = cfxp.previousTabMode.get();	
				switch(tabMode) {
					case 0: // tabs on top glass
						cfxg.glass.stop();
						break;
					case 2: // tabs on top no glass
						cfxs.style.remove(cfxt._tab[0]);
						break;
					case 1: // basic mode
						cfxs.style.remove(cfxt._tab[1]);
						break;
					default:
						break;
				}
			}
		},

		tabWidth: function() {
			var tabs = gBrowser.mTabContainer;
			for(var i = 0; i < tabs.childNodes.length; i++) {
				var aTab = tabs.childNodes[i];
				if(aTab.hasAttribute("pin") || aTab.hasAttribute("faviconize")) {
					aTab.maxWidth = "";
					aTab.minWidth = "";
				} else {
					aTab.maxWidth = cfxp.tabMaxWidth.get();
					aTab.minWidth = cfxp.tabMinWidth.get();
				}
			}
		},
		
		tabClose: {
			update: function() {
				cfxt.tabClose.reset();
				cfxt.tabClose.set();
			},
			set: function() {
				var mode = cfxp.tabClose.get();
				cfxp.uTabClose.set(mode);
				switch(mode) {
					case 0: // Close button on Active tab only
						cfxs.style.apply(cfxt._tabClose[0]);
						break;
					case 1: // No visible close button
						cfxs.style.apply(cfxt._tabClose[1]);
						break;
					case 2: // close button on tabbar only
						cfxs.style.apply(cfxt._tabClose[1]);
						cfxp.mTabClose.set(3);
						break;
					default:
						break;
				}
				if(mode != 2) { // reset mozilla's pref
					if(cfxp.mTabClose.get() != 1) {
						cfxp.mTabClose.set(1);
					}
				}
			},
			reset: function() {
				var mode = cfxp.uTabClose.get();
				switch(mode) {
					case 0: // Close button on Active tab only
						cfxs.style.remove(cfxt._tabClose[0]);
						break;
					case 1: // No visible close button
						cfxs.style.remove(cfxt._tabClose[1]);
						break;
					case 2: // close button on tabbar only
						cfxs.style.remove(cfxt._tabClose[1]);
						break;
					default:
						break;
				}
			}
		}
	};
	
	var cfxd = {
		init: function() {
			cfxd.fullScreen();
			cfxd.urlBar.init();
			cfxd.findBar();
			cfxd.menus.init();
			cfxd.dropMarker();
			cfxd.windowDrag.init();
		},
		
		unInit: function() {
			document.getElementById("FindToolbar").removeEventListener("DOMAttrModified", cfxd.handleEvent, false);
			document.getElementById("nav-bar").removeEventListener("DOMAttrModified", cfxd.handleEvent, false);
		},
		
		menus: {
			init: function() {
				cfxd.menus.toolsMenu();
				cfxd.menus.historyMenu();
				cfxd.menus.bookmarksMenu();
			},
			toolsMenu: function() {
				if(cfxp.toolsMenu.get()) {
					var cfxhPrefs = document.createElement("menuitem");
					cfxhPrefs.setAttribute("id", "cfxh-prefs");
					cfxhPrefs.setAttribute("label", document.getElementById("cfxhStrings").getString("cfxh-prefs"));
					cfxhPrefs.setAttribute("oncommand", "cfxu.openCfxDialog();");
					document.getElementById("menu_ToolsPopup").insertBefore(cfxhPrefs, document.getElementById("menu_preferences"));
				} else {
					if(document.getElementById("cfxh-prefs")) {
						document.getElementById("menu_ToolsPopup").removeChild(document.getElementById("cfxh-prefs"));
					}
				}
			},
			historyMenu: function() {
				if(cfxp.historyMenu.get()) {
					document.getElementById("historyseperator").hidden = false;
					document.getElementById("cfxtoolsbutton-menupopup").insertBefore(document.getElementById("history-menu"), document.getElementById("historyseperator"));
				} else {
					if(!document.getElementById("historyseperator").getAttribute("hidden")) {
						if(cfxp.bookmarksMenu.get()) {
							document.getElementById("main-menubar").insertBefore(document.getElementById("history-menu"), document.getElementById("tools-menu"));
						} else {
							document.getElementById("main-menubar").insertBefore(document.getElementById("history-menu"), document.getElementById("bookmarksMenu"));
						}
					}
				}
			},
			bookmarksMenu: function() {
				if(cfxp.bookmarksMenu.get()) {
					document.getElementById("historyseperator").hidden = false;
					document.getElementById("cfxtoolsbutton-menupopup").insertBefore(document.getElementById("bookmarksMenu"), document.getElementById("historyseperator"));
				} else {
					if(!document.getElementById("historyseperator").getAttribute("hidden")) {
						document.getElementById("historyseperator").hidden = true;
						document.getElementById("main-menubar").insertBefore(document.getElementById("bookmarksMenu"), document.getElementById("tools-menu"));
					}
				}
			}
		},
		
		dropMarker: function() {
			if(cfxp.dropMarker.get()) {
				document.getElementById("back-forward-dropmarker").hidden = false;
			} else {
				document.getElementById("back-forward-dropmarker").hidden = true;
			}
		},
		
		findBar: function() {
			if(cfxp.findBar.get()) {
				document.getElementById("main-window").setAttribute("findbar", "true");
				if(!document.getElementById("main-window").hasAttribute("findbarhidden")) {
					document.getElementById("main-window").setAttribute("findbarhidden", "true");
				}
				document.getElementById("main-window").insertBefore(document.getElementById("FindToolbar"), document.getElementById("browser"));
				document.getElementById("FindToolbar").addEventListener("DOMAttrModified", cfxd.handleEvent, false);
			} else {
				if(document.getElementById("main-window").hasAttribute("findbar")) {
					document.getElementById("FindToolbar").removeEventListener("DOMAttrModified", cfxd.handleEvent, false);
					document.getElementById("browser-bottombox").insertBefore(document.getElementById("FindToolbar"), document.getElementById("browser-bottombox").firstChild);
				}
				document.getElementById("main-window").setAttribute("findbar", false);
			}
		},

		fullScreen: function() {
			document.getElementById("main-window").setAttribute("inFullscreen", "false");
			document.getElementById("nav-bar").addEventListener("DOMAttrModified", cfxd.handleEvent, false);
		},
		
		urlBar: {
			_identityBox: ["chrome://browser/skin/identitybox/identityboxdefault.css","chrome://browser/skin/identitybox/identityboxcfx.css"],
			_starButton: ["chrome://browser/skin/starbutton/starbuttondefault.css","chrome://browser/skin/starbutton/starbuttoncfx.css"],
			_firstRun: true,
			init: function() {
				if(cfxp.urlbar.get()) {
					document.getElementById("main-window").setAttribute("urlbarui", "true");
					cfxd.urlBar.identityBox.apply(true);
					cfxd.urlBar.starButton.apply(true);
				} else {
					document.getElementById("main-window").setAttribute("urlbarui", "false");
					cfxd.urlBar.identityBox.apply(false);
					cfxd.urlBar.starButton.apply(false);
				}
			},
			identityBox: {
				apply: function(mode) {
					if(!cfxd.urlBar._firstRun) {
						cfxd.urlBar.identityBox.remove(mode);
						cfxd.urlBar._firstRun = false;
					}
					if(mode) {
						document.getElementById("urlbar-icons").insertBefore(document.getElementById("identity-box"), document.getElementById("feed-button"));
						cfxs.style.apply(cfxd.urlBar._identityBox[1]);
					} else {
						document.getElementById("urlbar").insertBefore(document.getElementById("identity-box"), document.getElementById("urlbar-icons"));
						cfxs.style.apply(cfxd.urlBar._identityBox[0]);
					}
				},
				remove: function(mode) {
					if(mode) {
						if(cfxs.isStyle(cfxd.urlBar._identityBox[0])) {
							cfxs.style.remove(cfxd.urlBar._identityBox[0]);
						}
					} else {
						if(cfxs.isStyle(cfxd.urlBar._identityBox[1])) {
							cfxs.style.remove(cfxd.urlBar._identityBox[1]);
						}
					}
				}
			},
			starButton: {
				apply: function(mode) {
					cfxd.urlBar.starButton.remove(mode);
					if(mode) {
						document.getElementById("urlbar").insertBefore(document.getElementById("star-button"), document.getElementById("urlbar-icons"));
						cfxs.style.apply(cfxd.urlBar._starButton[1]);
					} else {
						document.getElementById("urlbar-icons").insertBefore(document.getElementById("star-button"), document.getElementById("go-button"));
						cfxs.style.apply(cfxd.urlBar._starButton[0]);
					}
				},
				remove: function(mode) {
					if(mode) {
						if(cfxs.isStyle(cfxd.urlBar._starButton[0])) {
							cfxs.style.remove(cfxd.urlBar._starButton[0]);
						}
					} else {
						if(cfxs.isStyle(cfxd.urlBar._starButton[1])) {
							cfxs.style.remove(cfxd.urlBar._starButton[1]);
						}
					}
				}
			}
		},

		windowDrag: {
			formX: null,
			formY: null,
			downX: null,
			downY: null,
			init: function() {
				gBrowser.mTabContainer.addEventListener("mousedown", cfxd.windowDrag.tabMouseDown, false);
			},
			tabMouseDown: function(e){
				if(windowState != STATE_NORMAL || e.target.localName != 'tabs' || e.button != 0) {
					return;
				}
				cfxd.windowDrag.downX = e.screenX;
				cfxd.windowDrag.formX = screenX;
				cfxd.windowDrag.downY = e.screenY;
				cfxd.windowDrag.formY = screenY;
				window.addEventListener("mousemove", cfxd.windowDrag.onMouseMove, false);
				window.addEventListener("mouseup", cfxd.windowDrag.onMouseUp, false);
			},
			onMouseUp: function(e){
				cfxd.windowDrag.downX = null;
				cfxd.windowDrag.downY = null;
				window.removeEventListener("mousemove", cfxd.windowDrag.onMouseMove, false);
				window.removeEventListener("mouseup", cfxd.windowDrag.OnMouseUp, false);
			},
			onMouseMove: function(e){
				window.moveTo(cfxd.windowDrag.formX + e.screenX - cfxd.windowDrag.downX, cfxd.windowDrag.formY + e.screenY - cfxd.windowDrag.downY);
			}
		},

		handleEvent: function(e) {
			if(e.type == "DOMAttrModified") {
				switch(e.attrName) {
					case "moz-collapsed":
						if(e.target.id == "nav-bar") {
							document.getElementById("main-window").setAttribute("inFullscreen", e.newValue);
						}
						break;
					case "collapsed":
						if(e.target.className == "tabbrowser-strip") {
							document.getElementById("main-window").setAttribute("popup", e.newValue);
						}
						break;
					case "hidden":
						if(e.target.id == "FindToolbar") {
							document.getElementById("main-window").setAttribute("findbarhidden", e.newValue);
						}
						break;
					default:
						break;
				}
			}
		}
	};
	
	var cfxp = {
		cfxhBranch: null,
		tabsBranch: null,
		pin: {def:true, get:function(){return gPrefService.getBoolPref("extensions.cfxh.enablePin")}, set:function(val){gPrefService.setBoolPref("extensions.cfxh.enablePin", val)}},
		tabMode: {def:0, get:function(){return gPrefService.getIntPref("extensions.cfxh.tabMode")}, set:function(val){gPrefService.setIntPref("extensions.cfxh.tabMode", val)}},
		previousTabMode: {def:0, get:function(){return gPrefService.getIntPref("extensions.cfxh.previousTabMode")}, set:function(val){gPrefService.setIntPref("extensions.cfxh.previousTabMode", val)}},
		urlbar: {def:true, get:function(){return gPrefService.getBoolPref("extensions.cfxh.enableUrlbar")}, set:function(val){gPrefService.setBoolPref("extensions.cfxh.enableUrlbar", val)}},
		firstRun: {def:true, get:function(){return gPrefService.getBoolPref("extensions.cfxh.firstRun")}, set:function(val){gPrefService.setBoolPref("extensions.cfxh.firstRun", val)}},
		disable: {def:false, get:function(){return gPrefService.getBoolPref("extensions.cfxh.disable")}, set:function(val){gPrefService.setBoolPref("extensions.cfxh.disable", val)}},	
		highLight: {def:false, get:function(){return gPrefService.getBoolPref("extensions.cfxh.highLight")}, set:function(val){gPrefService.setBoolPref("extensions.cfxh.highLight", val)}},
		findBar: {def:true, get:function(){return gPrefService.getBoolPref("extensions.cfxh.findBarOnTop")}, set:function(val){gPrefService.setBoolPref("extensions.cfxh.findBarOnTop", val)}},
		tabClose: {def:3, get:function(){return gPrefService.getIntPref("extensions.cfxh.tabClose")}, set:function(val){gPrefService.setIntPref("extensions.cfxh.tabClose", val)}},
		uTabClose: {def:3, get:function(){return gPrefService.getIntPref("extensions.cfxh.unLoadTabClose")}, set:function(val){gPrefService.setIntPref("extensions.cfxh.unLoadTabClose", val)}},
		toolsMenu: {def:true, get:function(){return gPrefService.getBoolPref("extensions.cfxh.enableMenu")}, set:function(val){gPrefService.setBoolPref("extensions.cfxh.enableMenu", val)}},
		historyMenu: {def:false, get:function(){return gPrefService.getBoolPref("extensions.cfxh.enableHistoryMenu")}, set:function(val){gPrefService.setBoolPref("extensions.cfxh.enableHistoryMenu", val)}},
		bookmarksMenu: {def:false, get:function(){return gPrefService.getBoolPref("extensions.cfxh.enableBookMarksMenu")}, set:function(val){gPrefService.setBoolPref("extensions.cfxh.enableBookMarksMenu", val)}},
		dropMarker: {def:true, get:function(){return gPrefService.getBoolPref("extensions.cfxh.enableDropmarker")}, set:function(val){gPrefService.setBoolPref("extensions.cfxh.enableDropmarker", val)}},
		dlsb: {def:false, get:function(){return gPrefService.getBoolPref("extensions.cfxh.enableDlsb")}, set:function(val){gPrefService.setBoolPref("extensions.cfxh.enableDlsb", val)}},
		glass: {def:false, get:function(){return gPrefService.getBoolPref("extensions.cfxh.enableGlass")}, set:function(val){gPrefService.setBoolPref("extensions.cfxh.enableGlass",val )}},
		resetTabWidth: {def:false, get:function(){return gPrefService.getBoolPref("extensions.cfxh.resetTabWidth")}, set:function(val){gPrefService.setBoolPref("extensions.cfxh.resetTabWidth", val)}},
		linkOpenNewWindow: {def:3, get:function(){return gPrefService.getIntPref("browser.linkopen_newwindow")}, set:function(val){gPrefService.setIntPref("browser.linkopen_newwindow", val)}},
		linkOpenNewWindowRestrict: {def:2, get:function(){return gPrefService.getIntPref("browser.linkopen_newwindow.restriction")}, set:function(val){gPrefService.setIntPref("browser.linkopen_newwindow.restriction", val)}},
		tabClipWidth: {def:140, get:function(){return gPrefService.getIntPref("browser.tabs.tabClipWidth")}, set:function(val){gPrefService.setIntPref("browser.tabs.tabClipWidth", val)}},
		insertRelatedAfterCurrent: {def:true, get:function(){return gPrefService.getBoolPref("browser.tabs.insertRelatedAfterCurrent")}, set:function(val){gPrefService.setBoolPref("browser.tabs.insertRelatedAfterCurrent", val)}},
		ctrlTabPreview: {def:false, get:function(){return gPrefService.getBoolPref("browser.ctrlTab.previews")}, set:function(val){gPrefService.setBoolPref("browser.ctrlTab.previews", val)}},
		allTabsPreview: {def:false, get:function(){return gPrefService.getBoolPref("browser.allTabs.previews")}, set:function(val){gPrefService.setBoolPref("browser.allTabs.previews", val)}},
		mTabClose: {def:1, get:function(){return gPrefService.getIntPref("browser.tabs.closeButtons")}, set:function(val){gPrefService.setIntPref("browser.tabs.closeButtons",val )}},
		theme: {get:function(){return gPrefService.getCharPref("general.skins.selectedSkin")}},
		tabMinWidth: {def:100, get:function(){return gPrefService.getIntPref("browser.tabs.tabMinWidth")}, set:function(val){gPrefService.setIntPref("browser.tabs.tabMinWidth", val)}},
		tabMaxWidth: {def:250, get:function(){return gPrefService.getIntPref("browser.tabs.tabMaxWidth")}, set:function(val){gPrefService.setIntPref("browser.tabs.tabMaxWidth", val)}},
		tabAutoHide: {def:false, get:function(){return gPrefService.getBoolPref("browser.tabs.autoHide")}, set:function(val){gPrefService.setBoolPref("browser.tabs.autoHide", val)}},
		closeWindowLastTab: {def:false, get:function(){return gPrefService.getBoolPref("browser.tabs.closeWindowWithLastTab")}, set:function(val){gPrefService.setBoolPref("browser.tabs.closeWindowWithLastTab", val)}},
		fission: {def:"#62ed50", get:function(){return gPrefService.getBoolPref("extensions.fission.color")}, set:function(val){gPrefService.setBoolPref("extensions.fission.color", val)}},
		
		resetDefaults: function() {
			cfxp.tabMinWidth.set(cfxp.tabMinWidth.def);
			cfxp.tabMaxWidth.set(cfxp.tabMaxWidth.def);
			cfxp.tabClipWidth.set(cfxp.tabClipWidth.def);
			cfxp.linkOpenNewWindow.set(cfxp.linkOpenNewWindow.def);
			cfxp.linkOpenNewWindowRestrict.set(cfxp.linkOpenNewWindowRestrict.def);
			cfxp.insertRelatedAfterCurrent.set(cfxp.insertRelatedAfterCurrent.def);
			cfxp.ctrlTabPreview.set(cfxp.ctrlTabPreview.def);
			cfxp.allTabsPreview.set(cfxp.allTabsPreview.def);
			cfxp.mTabClose.set(cfxp.mTabClose.def);
			cfxp.tabAutoHide.set(cfxp.tabAutoHide.def);
			cfxp.closeWindowLastTab.set(cfxp.closeWindowLastTab.def);
			cfxu.restartApp();
		},

		register: function() {
			this.cfxhBranch = gPrefService.getBranch("extensions.cfxh.");
			this.tabsBranch = gPrefService.getBranch("browser.tabs.");
			this.cfxhBranch.QueryInterface(Ci.nsIPrefBranch2);
			this.tabsBranch.QueryInterface(Ci.nsIPrefBranch2);
			this.cfxhBranch.addObserver("", this, false);
			this.tabsBranch.addObserver("", this, false);
		},
		
		unRegister: function() {
			this.cfxhBranch.removeObserver("", this, false);
			this.tabsBranch.removeObserver("", this, false);
		},
		
		observe: function(subject, topic, data) {
			if(topic != "nsPref:changed"){return;}
			switch(data) {
				case "enableUrlbar":
					cfxd.urlBar.init();
					break;
				case "tabMode":
					cfxt.tabStyle.remove();
					cfxt.tabStyle.apply();
					break;
				case "tabClose":
					cfxt.tabClose.update();
					break;
				case "tabMinWidth":
				case "tabMaxWidth":
					cfxt.tabWidth();
					break;
				case "highLight":
					cfxt.highLight(gBrowser.selectedTab, cfxp.highLight.get());
					break;
				case "findBarOnTop":
					cfxd.findBar();
					break;
				case "enableMenu":
					cfxd.menus.toolsMenu();
					break;
				case "enableHistoryMenu":
					cfxd.menus.historyMenu();
					break;
				case "enableBookMarksMenu":
					cfxd.menus.bookmarksMenu();
					break;
				case "enableDropmarker":
					cfxd.dropMarker();
					break;
				case "enableDlsb":
					cfxe.dlsb();
					break;
				case "insertRelatedAfterCurrent":
					cfxu.prompt();
					break;
				case "pin":
					cfxt.pin.tab();
					break;
				default:
					break;
			}
		}
	};

	var cfxg = {
		_dwm: null,
		onGlassChange: {
			handleEvent: function(e) {
				if(cfxg._dwm.GlassEnabled) {
					cfxs.style.apply(cfxt._tab[2]);
					gBrowser.addEventListener("resize", cfxg.onWindowSized, false);
					cfxg.onWindowSized.handleEvent();
				} else {
					cfxs.style.remove(cfxt._tab[2]);
					gBrowser.removeEventListener("resize", cfxg.onWindowSized, false);
				}
			}
		},
		
		onWindowSized: {
			_disabled: false,
			handleEvent: function(e) {
				if(window.fullScreen) {
					if(!cfxg.onWindowSized._disabled) {
						cfxs.style.remove(cfxt._tab[2]);
						cfxs.style.apply(cfxt._tab[0]);
						cfxg._dwm.ExtendFrameIntoWindow(0, 0, 0, 0);
						cfxg.onWindowSized._disabled = true;
					}
				} else if(windowState == STATE_MAXIMIZED) {
					if(!cfxs.isStyle(cfxt._tab[2])) {
						cfxhs.add(cfxt._tab[2]);
					}
					var top = document.getElementById("glassBar").boxObject.y + document.getElementById("glassBar").boxObject.height;
					cfxg._dwm.ExtendFrameIntoWindow(top, 0, 0, 0);
					cfxg.onWindowSized._disabled = false;
				} else {
					if(!cfxs.isStyle(cfxt._tab[2])) {
						cfxs.style.apply(cfxt._tab[2]);
					}
					var top = document.getElementById("glassBar").boxObject.y + document.getElementById("glassBar").boxObject.height;
					cfxg._dwm.ExtendFrameIntoWindow(top, 2, 2, 2);
					cfxg.onWindowSized._disabled = false;
				}
			}
		},
		
		glass: {
			start: function() {
				cfxg._dwm = Cc["@sixxgate.com/DwmCalls/CDwmCalls;1"].createInstance().QueryInterface(Ci.IDwmCalls);
				cfxg._dwm.AttachToWindow(window);
				cfxg._dwm.OnGlassChange = cfxg.onGlassChange;
				cfxg.onGlassChange.handleEvent();
			},
			stop: function() {
				cfxs.style.remove(cfxt._tab[2]);
				cfxg._dwm.OnGlassChange = null;
				gBrowser.removeEventListener("resize", cfxg.onWindowSized, false);
				cfxg.onWindowSized.disable = false;
				cfxg._dwm.ExtendFrameIntoWindow(0, 0, 0, 0);
			}
		}
    };

	var cfxs = {
		style: {
			apply: function(style) {
				cfxs.sss.loadAndRegisterSheet(cfxs.ios.newURI(style, null, null), cfxs.sss.USER_SHEET);
			},
			remove: function(style) {
				cfxs.sss.unregisterSheet(cfxs.ios.newURI(style, null, null), cfxs.sss.USER_SHEET);
			}
		},
		
		isStyle: function(style) {
			return cfxs.sss.sheetRegistered(cfxs.ios.newURI(style, null, null), cfxs.sss.USER_SHEET);
		},
		
		sss: Cc["@mozilla.org/content/style-sheet-service;1"].getService(Ci.nsIStyleSheetService),
		ios: Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService)
	};

	var cfxe = {
		_dlsb: ["Download Statusbar","chrome://global/skin/extsupport/dlsb/dlsb.css"],
		_yoono: ["Yoono","chrome://global/skin/extsupport/yoono/yoonodefault.css","chrome://global/skin/extsupport/yoono/yoonocfx.css"],
		init: function() {
			cfxe.dlsb();
			cfxe.yoono();
		},
		
		isExtension: function(ext) {
			var extManager = Cc["@mozilla.org/extensions/manager;1"].getService(Ci.nsIExtensionManager);
			var extensions = extManager.getItemList(Ci.nsIUpdateItem.TYPE_EXTENSION, { });
			for(var i in extensions) {
				if(extensions[i].name.indexOf(ext) === 0) {
					return true;
				}
			}
			return false;
		},

		dlsb: function() {
			if(cfxe.isExtension(cfxe._dlsb[0])) {
				if(document.getElementById("downbarHolder") === null) {
					cfxp.dlsb.set(cfxp.dlsb.def);
					if(cfxs.isStyle(cfxe._dlsb[1])) {
						cfxs.style.remove(cfxe._dlsb[1]);
					}
				}
			} else {
				cfxp.dlsb.set(true);
				cfxs.style.apply(cfxe._dlsb[1]);
			}
		},
			
		yoono: function() {
			if(this.isExtension(cfxe._yoono[0])) {
				if(document.getElementById("urlbar-account-selector")) {
					if(cfxp.urlbar.get()) {
						cfxs.style.apply(cfxe._yoono[2]);
					} else {
						cfxs.style.apply(cfxe._yoono[1]);
					}
				}
			}
		}
	};

	var cfxu = {
		customizeDone: function() {
			try {
				BrowserToolboxCustomizeDone(true);
			} catch(err) {}
			if(cfxp.urlbar.get() === false) {
				document.getElementById("cfx-identity-box") && (window.gIdentityHandler._identityBox = document.getElementById("cfx-identity-box"));
			}
		},

		restartApp: function() {
			if(!window.canQuitApplication()) {
				return;
			}
			Cc['@mozilla.org/toolkit/app-startup;1'].getService(Ci.nsIAppStartup).quit(Ci.nsIAppStartup.eRestart | Ci.nsIAppStartup.eAttemptQuit);
		},
		
		vacuum: function() {
			Cc["@mozilla.org/browser/nav-history-service;1"].getService(Ci.nsPIPlacesDatabase).DBConnection.executeSimpleSQL("VACUUM");
			Cc["@mozilla.org/alerts-service;1"].getService(Ci.nsIAlertsService).
				showAlertNotification("chrome://cfxhelper/skin/vacuum.png", document.getElementById("cfxhStrings").
				getString("vac"), document.getElementById("cfxhStrings").getString("vacced"), false, "vacuumed", null);
		},
		
		prompt: function() {
			var pmtService = Cc["@mozilla.org/embedcomp/prompt-service;1"].getService(Ci.nsIPromptService);
			pmtService.alert(window, document.getElementById("cfxhStrings").getString("cfxextreme"), document.getElementById("cfxhStrings").getString("pmtmsg"))
		},
		
		osPlatform: function() {
			return Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime).OS;
		},

		openCfxDialog: function() {
			openDialog('chrome://cfxhelper/content/preferences/preferences.xul', '', 'chrome,titlebar,toolbar,centerscreen,modal');
		}
	};
	
window.addEventListener("load", 
	function() {
		var currentTheme = cfxp.theme.get();
		switch(currentTheme) {
			case "cfxe":
			case "cfxec":
				document.getElementById("main-window").setAttribute("theme", currentTheme);
				document.getElementById("main-window").setAttribute("osver", cfxu.osPlatform());
				if(cfxp.disable.get()) { cfxp.disable.set(false); }
				if(cfxp.resetTabWidth.get()) { cfxp.resetTabWidth.set(false); }
				cfxp.tabAutoHide.set(false);
				cfxp.closeWindowLastTab.set(false);
				cfxp.register();
				cfxt.init();
				cfxd.init();
				cfxe.init();
				if(cfxp.firstRun.get()) {
					cfxp.fission.set(cfxp.fission.def);
					cfxp.firstRun.set(false);
				}
				cfxu.customizeDone();
				break;
			default:
				if(!cfxp.disable.get()) { cfxp.disable.set(true); }
				if(document.getElementById("cfx-tools-button") !== null) {
					document.getElementById("cfx-tools-button").disabled = true;
					document.getElementById("cfx-tools-button").hidden = true;
				}
				if(document.getElementById("cfx-control-button") !== null) {
					document.getElementById("cfx-control-button").disabled = true;
					document.getElementById("cfx-control-button").hidden = true;
				}
				if(!cfxp.resetTabWidth.get()) {
					cfxp.resetTabWidth.set(true);
					cfxp.resetDefaults();
				}
				break;
		}
	}, false);

window.addEventListener("unload",
	function() {
		cfxp.unRegister();
		cfxd.unInit();
		cfxt.unInit();
	}, false);